鼠标控制摄像机
//效果就是左右鼠标晃动改变人物x轴的轴向
//——–上下晃动改变摄像机围绕人朝向
using UnityEngine;
using System.Collections;
public class CameraWhirl : MonoBehaviour {
public Transform target;
//旋转度数
float rotationX = 0F;
float rotationY = 0F;
//灵敏度
public float sensitivityX = 5F;
public float sensitivityY = 5F;
//旋转范围
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -20F;
public float maximumY = 80F;
//相机与主角的距离
public float distance=-3.0F;
Quaternion originalRotation;
Vector3 angles ;
// Use this for initialization
void Start () {
distance=-3.0F;
originalRotation =target.transform.localRotation;
angles = transform.eulerAngles;
}
// Update is called once per frame
void Update () {
//x轴 //——–左右晃动改变人物x轴的轴向
rotationX+=Input.GetAxis("Mouse X")*sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
target.transform.localRotation = originalRotation * xQuaternion;
//~ print("X:"+Input.GetAxis("Mouse X")+" "+"Y:"+Input.GetAxis("Mouse Y"));
//~ print("Q4:"+target.transform.localRotation);
//Y轴 //——–上下晃动改变摄像机围绕人朝向
rotationY+=Input.GetAxis("Mouse Y")*sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion rotation = Quaternion.Euler(rotationY,rotationX, 0);
print("rotationY:"+rotationY+" rotation:"+rotation);
//~ Vector3 V3= new Vector3(target.transform.position.x,target.transform.position.y, target.transform.position.z-distance);
//~ transform.position =V3;
transform.rotation = rotation;
Vector3 V3=new Vector3(0.0F, 0.0F, distance);
transform.position =rotation*V3+target.position;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
本条目发布于2010年6月30日。属于未分类分类。